package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzweb.annotation.OrderLog;
import com.jinzhi.jzweb.domain.vo.JazCoursesOrdeVo;
import com.jinzhi.jzweb.domain.vo.JazGroupActivityVo;
import com.jinzhi.jzweb.domain.vo.JazGroupOrderVo;
import com.jinzhi.jzweb.service.JazCoursesOrderService;
import com.jinzhi.jzweb.service.JazGroupOrderService;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazGroupActivityDO;
import com.jinzhi.jzweb.service.JazGroupActivityService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 团购活动拼团表
 * </pre>
 * <small> 2024-05-11 13:54:15 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazGroupActivity")
public class JazGroupActivityController extends BaseController {
	@Autowired
	private JazGroupActivityService jazGroupActivityService;
	@Autowired
	private JazCoursesOrderService jazCoursesOrderService;
	@Autowired
	private JazGroupOrderService jazGroupOrderService;

	@ApiOperation(value = "团购列表页面入口", notes = "团购列表页面入口")
	@ApiImplicitParams({
	})
	@ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@GetMapping("")
	@RequiresPermissions("jzweb:jazGroupActivity:jazGroupActivity")
	String JazGroupActivity(Model model){
		model.addAttribute("type", "拼团");
	    return "jzweb/jazGroupActivity/jazGroupActivity";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:jazGroupActivity:jazGroupActivity")
	public Result<Page<JazGroupActivityDO>> list(JazGroupActivityDO jazGroupActivityDTO){
		// 查询列表数据
		Page<JazGroupActivityDO> page = jazGroupActivityService.selectPage(getPage(JazGroupActivityDO.class),
					jazGroupActivityService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				JazGroupActivityDO obj = (JazGroupActivityDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}

	/**
	 *  拼团订单列表查询
	 * @return
	 */
	@GetMapping("/selGroupActivityList")
	@ResponseBody
	public Result<Page<JazGroupActivityDO>> selGroupProdList(@RequestParam(value = "name",required = false) String name,
									  @RequestParam(value = "mobile",required = false) String mobile,
									  @RequestParam("current")int current,
									  @RequestParam("size")int size,
									  @RequestParam(value = "createTime",required = false) String createTime,
									  @RequestParam(value = "endTime",required = false)String endTime)  {

		return jazGroupActivityService.selGroupActivityList(name,mobile,current,size,createTime,endTime);
	}

	/**
	 * 拼团详情
	 * @return
	 */
	@GetMapping("/getInfo")
	public Result<?> getInfo(@RequestParam("id")Long id )  {
		JazGroupActivityVo vo=jazGroupActivityService.getInfo(id);
		return Result.ok(vo);

	}
	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazGroupActivity:add")
	String add(){
	    return "jzweb/jazGroupActivity/add";
	}

	@GetMapping("/info/{id}/{itemId}/{type}")
	String info(@PathVariable("id") Long id,@PathVariable("itemId") Long itemId,@PathVariable("type") String type,Model model){
		if(type.equals("拼团")){
			JazGroupActivityVo jazGroupActivity = jazGroupActivityService.getInfo(id);
			model.addAttribute("myData", jazGroupActivity);
		}else if (type.equals("课程")){
			JazCoursesOrdeVo vo=jazCoursesOrderService.getInfo(id);
			model.addAttribute("myData", vo);
		}else if (type.equals("订单")){
			JazGroupOrderVo vo=jazGroupOrderService.getInfo(id);
			model.addAttribute("myData", vo);
		}
		model.addAttribute("type", type);
		model.addAttribute("itemId", itemId);
		model.addAttribute("id", id);
	    return "jzweb/jazGroupActivity/info";
	}
	
	@Log(value = "添加团购活动拼团",sysModule = 102,operationType = 1)
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazGroupActivity:add")
	public Result<String> save( JazGroupActivityDO jazGroupActivity){
		jazGroupActivityService.insert(jazGroupActivity);
        return Result.ok();
	}
	@OrderLog(value = "修改团购活动拼团",sysModule = 102,operationType = 3)
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:jazGroupActivity:edit")
	public Result<String>  update( JazGroupActivityDO jazGroupActivity){
		boolean update = jazGroupActivityService.updateById(jazGroupActivity);
		return update ? Result.ok() : Result.fail();
	}
	
	@OrderLog(value = "删除团购活动拼团",sysModule = 102,operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazGroupActivity:remove")
	public Result<String>  remove( Long id){
		jazGroupActivityService.deleteById(id);
        return Result.ok();
	}
	
	@OrderLog(value = "批量删除团购活动拼团",sysModule = 102,operationType = 2)
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazGroupActivity:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		jazGroupActivityService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
